package com.wei.czz.framework.admin.controller;

import com.wei.czz.common.annotation.Auth;
import com.wei.czz.common.annotation.Resubmit;
import com.wei.czz.common.dto.admin.role.RoleDto;
import com.wei.czz.common.jsr303.group.OrderSearch;
import com.wei.czz.common.property.BatchProperty;
import com.wei.czz.common.tool.PageDto;
import com.wei.czz.common.tool.Result;
import com.wei.czz.common.vo.admin.role.BindRoleVo;
import com.wei.czz.common.vo.admin.roleGroup.RoleGroupFormVo;
import com.wei.czz.framework.admin.manager.RoleGroupManager;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wei
 * date: 2024-10-22 16:46:05
 * className: RoleSpaceController 系统角色授权组与角色关联请求接口类
 * version: 1.0
 * description:
 */
@RestController
@RequestMapping("/sys/role-group")
@AllArgsConstructor
public class RoleGroupController {

    private static final Logger log = LoggerFactory.getLogger(RoleGroupController.class);

    private final BatchProperty batchProperty;

    private final RoleGroupManager roleGroupManager;

    /**
     * 分页获取角色授权组关联的角色接口
     * @param roleGroupFormVo 参数对象
     * @return 结果
     */
    @PostMapping("/page/list")
    @Auth(permit = "system:role-space:save")
    public Result<PageDto<RoleDto>> getPageList(@Validated(OrderSearch.class) @RequestBody RoleGroupFormVo roleGroupFormVo) {
        log.info("开始分页获取角色授权组关联的角色业务。{}", roleGroupFormVo);

        PageDto<RoleDto> pageDto = roleGroupManager.getPageList(roleGroupFormVo);

        log.info("分页获取角色授权组关联的角色业务结束");
        return Result.success(pageDto);
    }

    /**
     * 角色授权组绑定角色接口
     * @param bindRoleVo 参数对象
     * @return 结果
     */
    @Resubmit(spEL = "'role-group-bind' + #userPo.userId", message = "角色授权组绑定角色")
    @PostMapping("/bind")
    @Auth(permit = "system:role-space:save")
    public Result<Object> bindRole(@Validated @RequestBody BindRoleVo bindRoleVo) {
        log.info("开始角色授权组绑定角色业务。{}", bindRoleVo);

        if (batchProperty.getMaxSize() < bindRoleVo.getRoleIdList().size()) {
            log.info("角色授权组绑定角色数量超限");
            return Result.clientFail("一次绑定角色，最多允许同时绑定" + batchProperty.getMaxSize() + "个角色");
        }

        roleGroupManager.bindRole(bindRoleVo);

        log.info("角色授权组绑定角色业务结束");
        return Result.success();
    }

    /**
     * 角色授权组解绑角色接口
     * @param bindRoleVo 参数对象
     * @return 结果
     */
    @Resubmit(spEL = "'role-group-unbind' + #userPo.userId", message = "角色授权组解绑角色")
    @PostMapping("/unbind")
    @Auth(permit = "system:role-space:save")
    public Result<Object> unbindRole(@Validated @RequestBody BindRoleVo bindRoleVo) {
        log.info("开始角色授权组解绑角色业务。{}", bindRoleVo);

        List<Long> roleIdList = bindRoleVo.getRoleIdList();
        if (batchProperty.getMaxSize() < roleIdList.size()) {
            log.warn("角色授权组解绑角色数量超限。batch.maxSize={} list.size={}", batchProperty.getMaxSize(), roleIdList.size());
            return Result.clientFail("一次绑定角色，最多允许同时解绑" + batchProperty.getMaxSize() + "个角色");
        }

        roleGroupManager.unbindRole(bindRoleVo);

        log.info("角色授权组解绑角色业务结束");
        return Result.success();
    }


}
